
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/simplify/simplify.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:06 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Simplify &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="simplify.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Hypergeometric Expansion" href="hyperexpand.html" />
    <link rel="prev" title="Simplify" href="index.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="hyperexpand.html" title="Hypergeometric Expansion"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Simplify"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Simplify</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Simplify</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="simplify">
<h1>Simplify<a class="headerlink" href="#simplify" title="Permalink to this headline">¶</a></h1>
<section id="id1">
<h2>simplify<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.simplify">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">simplify</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">expr</span></em>, <em class="sig-param"><span class="pre">ratio=1.7</span></em>, <em class="sig-param"><span class="pre">measure=&lt;function</span> <span class="pre">count_ops&gt;</span></em>, <em class="sig-param"><span class="pre">rational=False</span></em>, <em class="sig-param"><span class="pre">inverse=False</span></em>, <em class="sig-param"><span class="pre">doit=True</span></em>, <em class="sig-param"><span class="pre">**kwargs</span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L411-L757"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.simplify" title="Permalink to this definition">¶</a></dt>
<dd><p>Simplifies the given expression.</p>
<p class="rubric">Explanation</p>
<p>Simplification is not a well defined term and the exact strategies
this function tries can change in the future versions of SymPy. If
your algorithm relies on “simplification” (whatever it is), try to
determine what you need exactly  -  is it powsimp()?, radsimp()?,
together()?, logcombine()?, or something else? And use this particular
function directly, because those are well defined and thus your algorithm
will be robust.</p>
<p>Nonetheless, especially for interactive use, or when you don’t know
anything about the structure of the expression, simplify() tries to apply
intelligent heuristics to make the input expression “simpler”.  For
example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">simplify</span><span class="p">,</span> <span class="n">cos</span><span class="p">,</span> <span class="n">sin</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">x</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">(x**2 + x)/(x*sin(y)**2 + x*cos(y)**2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">x + 1</span>
</pre></div>
</div>
<p>Note that we could have obtained the same result by using specific
simplification functions:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">trigsimp</span><span class="p">,</span> <span class="n">cancel</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">(x**2 + x)/x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cancel</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
<span class="go">x + 1</span>
</pre></div>
</div>
<p>In some cases, applying <a class="reference internal" href="#sympy.simplify.simplify.simplify" title="sympy.simplify.simplify.simplify"><code class="xref py py-func docutils literal notranslate"><span class="pre">simplify()</span></code></a> may actually result in some more
complicated expression. The default <code class="docutils literal notranslate"><span class="pre">ratio=1.7</span></code> prevents more extreme
cases: if (result length)/(input length) &gt; ratio, then input is returned
unmodified.  The <code class="docutils literal notranslate"><span class="pre">measure</span></code> parameter lets you specify the function used
to determine how complex an expression is.  The function should take a
single argument as an expression and return a number such that if
expression <code class="docutils literal notranslate"><span class="pre">a</span></code> is more complex than expression <code class="docutils literal notranslate"><span class="pre">b</span></code>, then
<code class="docutils literal notranslate"><span class="pre">measure(a)</span> <span class="pre">&gt;</span> <span class="pre">measure(b)</span></code>.  The default measure function is
<a class="reference internal" href="../core.html#sympy.core.function.count_ops" title="sympy.core.function.count_ops"><code class="xref py py-func docutils literal notranslate"><span class="pre">count_ops()</span></code></a>, which returns the total number of operations in the
expression.</p>
<p>For example, if <code class="docutils literal notranslate"><span class="pre">ratio=1</span></code>, <code class="docutils literal notranslate"><span class="pre">simplify</span></code> output can’t be longer
than input.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span><span class="p">,</span> <span class="n">simplify</span><span class="p">,</span> <span class="n">count_ops</span><span class="p">,</span> <span class="n">oo</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">+</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
<p>Since <code class="docutils literal notranslate"><span class="pre">simplify(root)</span></code> would result in a slightly longer expression,
root is returned unchanged instead:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">ratio</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="n">root</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">ratio=oo</span></code>, simplify will be applied anyway:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">count_ops</span><span class="p">(</span><span class="n">simplify</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">ratio</span><span class="o">=</span><span class="n">oo</span><span class="p">))</span> <span class="o">&gt;</span> <span class="n">count_ops</span><span class="p">(</span><span class="n">root</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Note that the shortest expression is not necessary the simplest, so
setting <code class="docutils literal notranslate"><span class="pre">ratio</span></code> to 1 may not be a good idea.
Heuristically, the default value <code class="docutils literal notranslate"><span class="pre">ratio=1.7</span></code> seems like a reasonable
choice.</p>
<p>You can easily define your own measure function based on what you feel
should represent the “size” or “complexity” of the input expression.  Note
that some choices, such as <code class="docutils literal notranslate"><span class="pre">lambda</span> <span class="pre">expr:</span> <span class="pre">len(str(expr))</span></code> may appear to be
good metrics, but have other problems (in this case, the measure function
may slow down simplify too much for very large expressions).  If you don’t
know what a good metric would be, the default, <code class="docutils literal notranslate"><span class="pre">count_ops</span></code>, is a good
one.</p>
<p>For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">log</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;a b&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="n">log</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">+</span> <span class="n">log</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">log</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">b</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">simplify</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h</span>
<span class="go">log(a*b**(1 - log(a)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">count_ops</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">8</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">count_ops</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>
<span class="go">5</span>
</pre></div>
</div>
<p>So you can see that <code class="docutils literal notranslate"><span class="pre">h</span></code> is simpler than <code class="docutils literal notranslate"><span class="pre">g</span></code> using the count_ops metric.
However, we may not like how <code class="docutils literal notranslate"><span class="pre">simplify</span></code> (in this case, using
<code class="docutils literal notranslate"><span class="pre">logcombine</span></code>) has created the <code class="docutils literal notranslate"><span class="pre">b**(log(1/a)</span> <span class="pre">+</span> <span class="pre">1)</span></code> term.  A simple way
to reduce this would be to give more weight to powers as operations in
<code class="docutils literal notranslate"><span class="pre">count_ops</span></code>.  We can do this by using the <code class="docutils literal notranslate"><span class="pre">visual=True</span></code> option:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">count_ops</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">visual</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">2*ADD + DIV + 4*LOG + MUL</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">count_ops</span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">visual</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">2*LOG + MUL + POW + SUB</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">my_measure</span><span class="p">(</span><span class="n">expr</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">POW</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;POW&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="c1"># Discourage powers by giving POW a weight of 10</span>
<span class="gp">... </span>    <span class="n">count</span> <span class="o">=</span> <span class="n">count_ops</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">visual</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">POW</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="gp">... </span>    <span class="c1"># Every other operation gets a weight of 1 (the default)</span>
<span class="gp">... </span>    <span class="n">count</span> <span class="o">=</span> <span class="n">count</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">Symbol</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">S</span><span class="o">.</span><span class="n">One</span><span class="p">))</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_measure</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">8</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_measure</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>
<span class="go">14</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mf">15.</span><span class="o">/</span><span class="mi">8</span> <span class="o">&gt;</span> <span class="mf">1.7</span> <span class="c1"># 1.7 is the default ratio</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">measure</span><span class="o">=</span><span class="n">my_measure</span><span class="p">)</span>
<span class="go">-log(a)*log(b) + log(a) + log(b)</span>
</pre></div>
</div>
<p>Note that because <code class="docutils literal notranslate"><span class="pre">simplify()</span></code> internally tries many different
simplification strategies and then compares them using the measure
function, we get a completely different result that is still different
from the input expression by doing this.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">rational=True</span></code>, Floats will be recast as Rationals before simplification.
If <code class="docutils literal notranslate"><span class="pre">rational=None</span></code>, Floats will be recast as Rationals but the result will
be recast as Floats. If rational=False(default) then nothing will be done
to the Floats.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">inverse=True</span></code>, it will be assumed that a composition of inverse
functions, such as sin and asin, can be cancelled in any order.
For example, <code class="docutils literal notranslate"><span class="pre">asin(sin(x))</span></code> will yield <code class="docutils literal notranslate"><span class="pre">x</span></code> without checking whether
x belongs to the set where this relation is true. The default is
False.</p>
<p>Note that <code class="docutils literal notranslate"><span class="pre">simplify()</span></code> automatically calls <code class="docutils literal notranslate"><span class="pre">doit()</span></code> on the final
expression. You can avoid this behavior by passing <code class="docutils literal notranslate"><span class="pre">doit=False</span></code> as
an argument.</p>
<p>Also, it should be noted that simplifying the boolian expression is not
well defined. If the expression prefers automatic evaluation (such as
<a class="reference internal" href="../core.html#sympy.core.relational.Eq" title="sympy.core.relational.Eq"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Eq()</span></code></a> or <a class="reference internal" href="../logic.html#sympy.logic.boolalg.Or" title="sympy.logic.boolalg.Or"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Or()</span></code></a>), simplification will return <code class="docutils literal notranslate"><span class="pre">True</span></code> or
<code class="docutils literal notranslate"><span class="pre">False</span></code> if truth value can be determined. If the expression is not
evaluated by default (such as <a class="reference internal" href="../assumptions/assume.html#sympy.assumptions.assume.Predicate" title="sympy.assumptions.assume.Predicate"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Predicate()</span></code></a>), simplification will
not reduce it and you should use <a class="reference internal" href="../assumptions/refine.html#sympy.assumptions.refine.refine" title="sympy.assumptions.refine.refine"><code class="xref py py-func docutils literal notranslate"><span class="pre">refine()</span></code></a> or <a class="reference internal" href="../assumptions/ask.html#sympy.assumptions.ask.ask" title="sympy.assumptions.ask.ask"><code class="xref py py-func docutils literal notranslate"><span class="pre">ask()</span></code></a>
function. This inconsistency will be resolved in future version.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="../assumptions/refine.html#sympy.assumptions.refine.refine" title="sympy.assumptions.refine.refine"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.assumptions.refine.refine</span></code></a></dt><dd><p>Simplification using assumptions.</p>
</dd>
<dt><a class="reference internal" href="../assumptions/ask.html#sympy.assumptions.ask.ask" title="sympy.assumptions.ask.ask"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.assumptions.ask.ask</span></code></a></dt><dd><p>Query for boolean expressions using assumptions.</p>
</dd>
</dl>
</div>
</dd></dl>

</section>
<section id="separatevars">
<h2>separatevars<a class="headerlink" href="#separatevars" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.separatevars">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">separatevars</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">[]</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dict</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">force</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L38-L109"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.separatevars" title="Permalink to this definition">¶</a></dt>
<dd><p>Separates variables in an expression, if possible.  By
default, it separates with respect to all symbols in an
expression and collects constant coefficients that are
independent of symbols.</p>
<p class="rubric">Explanation</p>
<p>If <code class="docutils literal notranslate"><span class="pre">dict=True</span></code> then the separated terms will be returned
in a dictionary keyed to their corresponding symbols.
By default, all symbols in the expression will appear as
keys; if symbols are provided, then all those symbols will
be used as keys, and any terms in the expression containing
other symbols or non-symbols will be returned keyed to the
string ‘coeff’. (Passing None for symbols will return the
expression in a dictionary keyed to ‘coeff’.)</p>
<p>If <code class="docutils literal notranslate"><span class="pre">force=True</span></code>, then bases of powers will be separated regardless
of assumptions on the symbols involved.</p>
<p class="rubric">Notes</p>
<p>The order of the factors is determined by Mul, so that the
separated expressions may not necessarily be grouped together.</p>
<p>Although factoring is necessary to separate variables in some
expressions, it is not necessary in all cases, so one should not
count on the returned factors being factored.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">alpha</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">separatevars</span><span class="p">,</span> <span class="n">sin</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">separatevars</span><span class="p">((</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="n">y</span><span class="p">)</span>
<span class="go">(x*y)**y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">separatevars</span><span class="p">((</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x**y*y**y</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">e</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">z</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">+</span><span class="mi">2</span><span class="o">*</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">separatevars</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="go">2*x**2*z*(sin(y) + 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">separatevars</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">symbols</span><span class="o">=</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="nb">dict</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">{&#39;coeff&#39;: 2*z, x: x**2, y: sin(y) + 1}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">separatevars</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">alpha</span><span class="p">],</span> <span class="nb">dict</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">{&#39;coeff&#39;: 2*z, alpha: 1, x: x**2, y: sin(y) + 1}</span>
</pre></div>
</div>
<p>If the expression is not really separable, or is only partially
separable, separatevars will do the best it can to separate it
by using factoring.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">separatevars</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">-x*(3*x - y - 1)</span>
</pre></div>
</div>
<p>If the expression is not separable then expr is returned unchanged
or (if dict=True) then None is returned.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">separatevars</span><span class="p">(</span><span class="n">eq</span><span class="p">)</span> <span class="o">==</span> <span class="n">eq</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">separatevars</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">symbols</span><span class="o">=</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="nb">dict</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="nthroot">
<h2>nthroot<a class="headerlink" href="#nthroot" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.nthroot">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">nthroot</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_len</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">4</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prec</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">15</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L1379-L1436"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.nthroot" title="Permalink to this definition">¶</a></dt>
<dd><p>Compute a real nth-root of a sum of surds.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>expr</strong> : sum of surds</p>
<p><strong>n</strong> : integer</p>
<p><strong>max_len</strong> : maximum number of surds passed as constants to <code class="docutils literal notranslate"><span class="pre">nsimplify</span></code></p>
</dd>
</dl>
<p class="rubric">Algorithm</p>
<p>First <code class="docutils literal notranslate"><span class="pre">nsimplify</span></code> is used to get a candidate root; if it is not a
root the minimal polynomial is computed; the answer is one of its
roots.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.simplify</span> <span class="kn">import</span> <span class="n">nthroot</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nthroot</span><span class="p">(</span><span class="mi">90</span> <span class="o">+</span> <span class="mi">34</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">7</span><span class="p">),</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">sqrt(7) + 3</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="kroneckersimp">
<h2>kroneckersimp<a class="headerlink" href="#kroneckersimp" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.kroneckersimp">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">kroneckersimp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L1211-L1261"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.kroneckersimp" title="Permalink to this definition">¶</a></dt>
<dd><p>Simplify expressions with KroneckerDelta.</p>
<p>The only simplification currently attempted is to identify multiplicative cancellation:</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">KroneckerDelta</span><span class="p">,</span> <span class="n">kroneckersimp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">i</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">kroneckersimp</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">KroneckerDelta</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="n">KroneckerDelta</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">i</span><span class="p">))</span>
<span class="go">1</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="besselsimp">
<h2>besselsimp<a class="headerlink" href="#besselsimp" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.besselsimp">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">besselsimp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L1264-L1376"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.besselsimp" title="Permalink to this definition">¶</a></dt>
<dd><p>Simplify bessel-type functions.</p>
<p class="rubric">Explanation</p>
<p>This routine tries to simplify bessel-type functions. Currently it only
works on the Bessel J and I functions, however. It works by looking at all
such functions in turn, and eliminating factors of “I” and “-1” (actually
their polar equivalents) in front of the argument. Then, functions of
half-integer order are rewritten using strigonometric functions and
functions of integer order (&gt; 1) are rewritten using functions
of low order.  Finally, if the expression was changed, compute
factorization of the result with factor().</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">besselj</span><span class="p">,</span> <span class="n">besseli</span><span class="p">,</span> <span class="n">besselsimp</span><span class="p">,</span> <span class="n">polar_lift</span><span class="p">,</span> <span class="n">I</span><span class="p">,</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">z</span><span class="p">,</span> <span class="n">nu</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">besselsimp</span><span class="p">(</span><span class="n">besselj</span><span class="p">(</span><span class="n">nu</span><span class="p">,</span> <span class="n">z</span><span class="o">*</span><span class="n">polar_lift</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)))</span>
<span class="go">exp(I*pi*nu)*besselj(nu, z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">besselsimp</span><span class="p">(</span><span class="n">besseli</span><span class="p">(</span><span class="n">nu</span><span class="p">,</span> <span class="n">z</span><span class="o">*</span><span class="n">polar_lift</span><span class="p">(</span><span class="o">-</span><span class="n">I</span><span class="p">)))</span>
<span class="go">exp(-I*pi*nu/2)*besselj(nu, z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">besselsimp</span><span class="p">(</span><span class="n">besseli</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="p">))</span>
<span class="go">sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">besselsimp</span><span class="p">(</span><span class="n">z</span><span class="o">*</span><span class="n">besseli</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span> <span class="o">+</span> <span class="n">z</span><span class="o">*</span><span class="p">(</span><span class="n">besseli</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="p">))</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="n">besseli</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">z</span><span class="p">))</span>
<span class="go">3*z*besseli(0, z)/2</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="hypersimp">
<h2>hypersimp<a class="headerlink" href="#hypersimp" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.hypersimp">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">hypersimp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L277-L321"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.hypersimp" title="Permalink to this definition">¶</a></dt>
<dd><p>Given combinatorial term f(k) simplify its consecutive term ratio
i.e. f(k+1)/f(k).  The input term can be composed of functions and
integer sequences which have equivalent representation in terms
of gamma special function.</p>
<p class="rubric">Explanation</p>
<p>The algorithm performs three basic steps:</p>
<ol class="arabic simple">
<li><p>Rewrite all functions in terms of gamma, if possible.</p></li>
<li><p>Rewrite all occurrences of gamma in terms of products
of gamma and rising factorial with integer,  absolute
constant exponent.</p></li>
<li><p>Perform simplification of nested fractions, powers
and if the resulting expression is a quotient of
polynomials, reduce their total degree.</p></li>
</ol>
<p>If f(k) is hypergeometric then as result we arrive with a
quotient of polynomials of minimal degree. Otherwise None
is returned.</p>
<p>For more information on the implemented algorithm refer to:</p>
<ol class="arabic simple">
<li><p>W. Koepf, Algorithms for m-fold Hypergeometric Summation,
Journal of Symbolic Computation (1995) 20, 399-417</p></li>
</ol>
</dd></dl>

</section>
<section id="hypersimilar">
<h2>hypersimilar<a class="headerlink" href="#hypersimilar" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.hypersimilar">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">hypersimilar</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">g</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L324-L343"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.hypersimilar" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <code class="docutils literal notranslate"><span class="pre">f</span></code> and <code class="docutils literal notranslate"><span class="pre">g</span></code> are hyper-similar.</p>
<p class="rubric">Explanation</p>
<p>Similarity in hypergeometric sense means that a quotient of
f(k) and g(k) is a rational function in <code class="docutils literal notranslate"><span class="pre">k</span></code>. This procedure
is useful in solving recurrence relations.</p>
<p>For more information see hypersimp().</p>
</dd></dl>

</section>
<section id="nsimplify">
<h2>nsimplify<a class="headerlink" href="#nsimplify" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.nsimplify">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">nsimplify</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">constants</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tolerance</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">full</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rational</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rational_conversion</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'base10'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L1439-L1575"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.nsimplify" title="Permalink to this definition">¶</a></dt>
<dd><p>Find a simple representation for a number or, if there are free symbols or
if <code class="docutils literal notranslate"><span class="pre">rational=True</span></code>, then replace Floats with their Rational equivalents. If
no change is made and rational is not False then Floats will at least be
converted to Rationals.</p>
<p class="rubric">Explanation</p>
<p>For numerical expressions, a simple formula that numerically matches the
given numerical expression is sought (and the input should be possible
to evalf to a precision of at least 30 digits).</p>
<p>Optionally, a list of (rationally independent) constants to
include in the formula may be given.</p>
<p>A lower tolerance may be set to find less exact matches. If no tolerance
is given then the least precise value will set the tolerance (e.g. Floats
default to 15 digits of precision, so would be tolerance=10**-15).</p>
<p>With <code class="docutils literal notranslate"><span class="pre">full=True</span></code>, a more extensive search is performed
(this is useful to find simpler numbers when the tolerance
is set low).</p>
<p>When converting to rational, if rational_conversion=’base10’ (the default), then
convert floats to rationals using their base-10 (string) representation.
When rational_conversion=’exact’ it uses the exact, base-2 representation.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">nsimplify</span><span class="p">,</span> <span class="n">sqrt</span><span class="p">,</span> <span class="n">GoldenRatio</span><span class="p">,</span> <span class="n">exp</span><span class="p">,</span> <span class="n">I</span><span class="p">,</span> <span class="n">pi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mi">4</span><span class="o">/</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">)),</span> <span class="p">[</span><span class="n">GoldenRatio</span><span class="p">])</span>
<span class="go">-2 + 2*GoldenRatio</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">((</span><span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">pi</span><span class="o">*</span><span class="n">I</span><span class="o">/</span><span class="mi">5</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)))</span>
<span class="go">1/2 - I*sqrt(sqrt(5)/10 + 1/4)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="n">I</span><span class="o">**</span><span class="n">I</span><span class="p">,</span> <span class="p">[</span><span class="n">pi</span><span class="p">])</span>
<span class="go">exp(-pi/2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="n">pi</span><span class="p">,</span> <span class="n">tolerance</span><span class="o">=</span><span class="mf">0.01</span><span class="p">)</span>
<span class="go">22/7</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mf">0.333333333333333</span><span class="p">,</span> <span class="n">rational</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">rational_conversion</span><span class="o">=</span><span class="s1">&#39;exact&#39;</span><span class="p">)</span>
<span class="go">6004799503160655/18014398509481984</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mf">0.333333333333333</span><span class="p">,</span> <span class="n">rational</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">1/3</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../core.html#sympy.core.function.nfloat" title="sympy.core.function.nfloat"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.core.function.nfloat</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="posify">
<h2>posify<a class="headerlink" href="#posify" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.posify">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">posify</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L216-L274"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.posify" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <code class="docutils literal notranslate"><span class="pre">eq</span></code> (with generic symbols made positive) and a
dictionary containing the mapping between the old and new
symbols.</p>
<p class="rubric">Explanation</p>
<p>Any symbol that has positive=None will be replaced with a positive dummy
symbol having the same name. This replacement will allow more symbolic
processing of expressions, especially those involving powers and
logarithms.</p>
<p>A dictionary that can be sent to subs to restore <code class="docutils literal notranslate"><span class="pre">eq</span></code> to its original
symbols is also returned.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">posify</span><span class="p">,</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">log</span><span class="p">,</span> <span class="n">solve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">posify</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;p&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="o">+</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">,</span> <span class="n">negative</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">(_x + n + p, {_x: x})</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">log</span><span class="p">(</span><span class="n">eq</span><span class="p">)</span><span class="o">.</span><span class="n">expand</span><span class="p">()</span>
<span class="go">log(1/x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">log</span><span class="p">(</span><span class="n">posify</span><span class="p">(</span><span class="n">eq</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">expand</span><span class="p">()</span>
<span class="go">-log(_x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">,</span> <span class="n">rep</span> <span class="o">=</span> <span class="n">posify</span><span class="p">(</span><span class="n">eq</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">log</span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="o">.</span><span class="n">expand</span><span class="p">()</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">rep</span><span class="p">)</span>
<span class="go">-log(x)</span>
</pre></div>
</div>
<p>It is possible to apply the same transformations to an iterable
of expressions:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">solve</span><span class="p">(</span><span class="n">eq</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="go">[-2, 2]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq_x</span><span class="p">,</span> <span class="n">reps</span> <span class="o">=</span> <span class="n">posify</span><span class="p">([</span><span class="n">eq</span><span class="p">,</span> <span class="n">x</span><span class="p">]);</span> <span class="n">eq_x</span>
<span class="go">[_x**2 - 4, _x]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">solve</span><span class="p">(</span><span class="o">*</span><span class="n">eq_x</span><span class="p">)</span>
<span class="go">[2]</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="logcombine">
<h2>logcombine<a class="headerlink" href="#logcombine" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.simplify.logcombine">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.simplify.</span></span><span class="sig-name descname"><span class="pre">logcombine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">force</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/simplify.py#L978-L1119"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.simplify.logcombine" title="Permalink to this definition">¶</a></dt>
<dd><p>Takes logarithms and combines them using the following rules:</p>
<ul class="simple">
<li><p>log(x) + log(y) == log(x*y) if both are positive</p></li>
<li><p>a*log(x) == log(x**a) if x is positive and a is real</p></li>
</ul>
<p>If <code class="docutils literal notranslate"><span class="pre">force</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then the assumptions above will be assumed to hold if
there is no assumption already in place on a quantity. For example, if
<code class="docutils literal notranslate"><span class="pre">a</span></code> is imaginary or the argument negative, force will not perform a
combination but if <code class="docutils literal notranslate"><span class="pre">a</span></code> is a symbol with no assumptions the change will
take place.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">log</span><span class="p">,</span> <span class="n">logcombine</span><span class="p">,</span> <span class="n">I</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">logcombine</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">log</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">-</span> <span class="n">log</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="go">a*log(x) + log(y) - log(z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">logcombine</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">log</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">-</span> <span class="n">log</span><span class="p">(</span><span class="n">z</span><span class="p">),</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">log(x**a*y/z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x,y,z&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">logcombine</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">log</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">-</span> <span class="n">log</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="go">log(x**a*y/z)</span>
</pre></div>
</div>
<p>The transformation is limited to factors and/or terms that
contain logs, so the result depends on the initial state of
expansion:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">I</span><span class="p">)</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">logcombine</span><span class="p">(</span><span class="n">eq</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="o">==</span> <span class="n">eq</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">logcombine</span><span class="p">(</span><span class="n">eq</span><span class="o">.</span><span class="n">expand</span><span class="p">(),</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">log(x**2) + I*log(x**3)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.simplify.simplify.posify" title="sympy.simplify.simplify.posify"><code class="xref py py-obj docutils literal notranslate"><span class="pre">posify</span></code></a></dt><dd><p>replace all symbols with symbols having positive assumptions</p>
</dd>
<dt><a class="reference internal" href="../core.html#sympy.core.function.expand_log" title="sympy.core.function.expand_log"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.core.function.expand_log</span></code></a></dt><dd><p>expand the logarithms of products and powers; the opposite of logcombine</p>
</dd>
</dl>
</div>
</dd></dl>

</section>
<section id="module-sympy.simplify.radsimp">
<span id="radsimp"></span><h2>Radsimp<a class="headerlink" href="#module-sympy.simplify.radsimp" title="Permalink to this headline">¶</a></h2>
<section id="id2">
<h3>radsimp<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.radsimp.radsimp">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.radsimp.</span></span><span class="sig-name descname"><span class="pre">radsimp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symbolic</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_terms</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">4</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/radsimp.py#L750-L1000"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.radsimp.radsimp" title="Permalink to this definition">¶</a></dt>
<dd><p>Rationalize the denominator by removing square roots.</p>
<p class="rubric">Explanation</p>
<p>The expression returned from radsimp must be used with caution
since if the denominator contains symbols, it will be possible to make
substitutions that violate the assumptions of the simplification process:
that for a denominator matching a + b*sqrt(c), a != +/-b*sqrt(c). (If
there are no symbols, this assumptions is made valid by collecting terms
of sqrt(c) so the match variable <code class="docutils literal notranslate"><span class="pre">a</span></code> does not contain <code class="docutils literal notranslate"><span class="pre">sqrt(c)</span></code>.) If
you do not want the simplification to occur for symbolic denominators, set
<code class="docutils literal notranslate"><span class="pre">symbolic</span></code> to False.</p>
<p>If there are more than <code class="docutils literal notranslate"><span class="pre">max_terms</span></code> radical terms then the expression is
returned unchanged.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">radsimp</span><span class="p">,</span> <span class="n">sqrt</span><span class="p">,</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">pprint</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">factor_terms</span><span class="p">,</span> <span class="n">fraction</span><span class="p">,</span> <span class="n">signsimp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.radsimp</span> <span class="kn">import</span> <span class="n">collect_sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">radsimp</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)))</span>
<span class="go">(2 - sqrt(2))/2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">Symbol</span><span class="p">,</span> <span class="s1">&#39;xy&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">e</span> <span class="o">=</span> <span class="p">((</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="p">(</span><span class="mi">2</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">8</span><span class="p">))</span><span class="o">*</span><span class="n">y</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">radsimp</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="go">sqrt(2)*(x + y)</span>
</pre></div>
</div>
<p>No simplification beyond removal of the gcd is done. One might
want to polish the result a little, however, by collecting
square root terms:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">r2</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r5</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ans</span> <span class="o">=</span> <span class="n">radsimp</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">y</span><span class="o">*</span><span class="n">r2</span> <span class="o">+</span> <span class="n">x</span><span class="o">*</span><span class="n">r2</span> <span class="o">+</span> <span class="n">a</span><span class="o">*</span><span class="n">r5</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">r5</span><span class="p">));</span> <span class="n">pprint</span><span class="p">(</span><span class="n">ans</span><span class="p">)</span>
<span class="go">    ___       ___       ___       ___</span>
<span class="go">  \/ 5 *a + \/ 5 *b - \/ 2 *x - \/ 2 *y</span>
<span class="go">------------------------------------------</span>
<span class="go">   2               2      2              2</span>
<span class="go">5*a  + 10*a*b + 5*b  - 2*x  - 4*x*y - 2*y</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">fraction</span><span class="p">(</span><span class="n">ans</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">factor_terms</span><span class="p">(</span><span class="n">signsimp</span><span class="p">(</span><span class="n">collect_sqrt</span><span class="p">(</span><span class="n">n</span><span class="p">))</span><span class="o">/</span><span class="n">d</span><span class="p">,</span> <span class="n">radical</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">        ___             ___</span>
<span class="go">      \/ 5 *(a + b) - \/ 2 *(x + y)</span>
<span class="go">------------------------------------------</span>
<span class="go">   2               2      2              2</span>
<span class="go">5*a  + 10*a*b + 5*b  - 2*x  - 4*x*y - 2*y</span>
</pre></div>
</div>
<p>If radicals in the denominator cannot be removed or there is no denominator,
the original expression will be returned.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">radsimp</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="go">sqrt(2)*x + sqrt(2)</span>
</pre></div>
</div>
<p>Results with symbols will not always be valid for all substitutions:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>
<span class="go">1/(2*b*sqrt(c))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">radsimp</span><span class="p">(</span><span class="n">eq</span><span class="p">)</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>
<span class="go">nan</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">symbolic=False</span></code>, symbolic denominators will not be transformed (but
numeric denominators will still be processed):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">radsimp</span><span class="p">(</span><span class="n">eq</span><span class="p">,</span> <span class="n">symbolic</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">1/(a + b*sqrt(c))</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="rad-rationalize">
<h3>rad_rationalize<a class="headerlink" href="#rad-rationalize" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.radsimp.rad_rationalize">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.radsimp.</span></span><span class="sig-name descname"><span class="pre">rad_rationalize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">num</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">den</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/radsimp.py#L1003-L1022"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.radsimp.rad_rationalize" title="Permalink to this definition">¶</a></dt>
<dd><p>Rationalize <code class="docutils literal notranslate"><span class="pre">num/den</span></code> by removing square roots in the denominator;
num and den are sum of terms whose squares are positive rationals.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.radsimp</span> <span class="kn">import</span> <span class="n">rad_rationalize</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rad_rationalize</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="mi">3</span><span class="p">)</span>
<span class="go">(-sqrt(3) + sqrt(6)/3, -7/9)</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="collect">
<h3>collect<a class="headerlink" href="#collect" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.radsimp.collect">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.radsimp.</span></span><span class="sig-name descname"><span class="pre">collect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">syms</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">evaluate</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exact</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">distribute_order_term</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/radsimp.py#L21-L451"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.radsimp.collect" title="Permalink to this definition">¶</a></dt>
<dd><p>Collect additive terms of an expression.</p>
<p class="rubric">Explanation</p>
<p>This function collects additive terms of an expression with respect
to a list of expression up to powers with rational exponents. By the
term symbol here are meant arbitrary expressions, which can contain
powers, products, sums etc. In other words symbol is a pattern which
will be searched for in the expression’s terms.</p>
<p>The input expression is not expanded by <a class="reference internal" href="#sympy.simplify.radsimp.collect" title="sympy.simplify.radsimp.collect"><code class="xref py py-func docutils literal notranslate"><span class="pre">collect()</span></code></a>, so user is
expected to provide an expression in an appropriate form. This makes
<a class="reference internal" href="#sympy.simplify.radsimp.collect" title="sympy.simplify.radsimp.collect"><code class="xref py py-func docutils literal notranslate"><span class="pre">collect()</span></code></a> more predictable as there is no magic happening behind the
scenes. However, it is important to note, that powers of products are
converted to products of powers using the <a class="reference internal" href="../core.html#sympy.core.function.expand_power_base" title="sympy.core.function.expand_power_base"><code class="xref py py-func docutils literal notranslate"><span class="pre">expand_power_base()</span></code></a>
function.</p>
<p>There are two possible types of output. First, if <code class="docutils literal notranslate"><span class="pre">evaluate</span></code> flag is
set, this function will return an expression with collected terms or
else it will return a dictionary with expressions up to rational powers
as keys and collected coefficients as values.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">S</span><span class="p">,</span> <span class="n">collect</span><span class="p">,</span> <span class="n">expand</span><span class="p">,</span> <span class="n">factor</span><span class="p">,</span> <span class="n">Wild</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
</pre></div>
</div>
<p>This function can collect symbolic coefficients in polynomials or
rational expressions. It will manage to find all integer or rational
powers of collection variable:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">a</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="go">c + x**2*(a + b) + x*(a - b)</span>
</pre></div>
</div>
<p>The same result can be achieved in dictionary form:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">a</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">evaluate</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">]</span>
<span class="go">a + b</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="n">x</span><span class="p">]</span>
<span class="go">a - b</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="n">S</span><span class="o">.</span><span class="n">One</span><span class="p">]</span>
<span class="go">c</span>
</pre></div>
</div>
<p>You can also work with multivariate polynomials. However, remember that
this function is greedy so it will care only about a single symbol at time,
in specification order:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">a</span><span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">])</span>
<span class="go">x**2*(y + 1) + x*y + y*(a + 1)</span>
</pre></div>
</div>
<p>Also more complicated expressions can be used as patterns:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sin</span><span class="p">,</span> <span class="n">log</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">),</span> <span class="n">sin</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">))</span>
<span class="go">(a + b)*sin(2*x)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">)),</span> <span class="n">x</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">x*(a + b)*log(x)</span>
</pre></div>
</div>
<p>You can use wildcards in the pattern:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">Wild</span><span class="p">(</span><span class="s1">&#39;w1&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span> <span class="o">-</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">w</span><span class="o">**</span><span class="n">y</span><span class="p">)</span>
<span class="go">x**y*(a - b)</span>
</pre></div>
</div>
<p>It is also possible to work with symbolic powers, although it has more
complicated behavior, because in this case power’s base and symbolic part
of the exponent are treated as a single symbol:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">c</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">c</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="go">a*x**c + b*x**c</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">c</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">c</span><span class="p">,</span> <span class="n">x</span><span class="o">**</span><span class="n">c</span><span class="p">)</span>
<span class="go">x**c*(a + b)</span>
</pre></div>
</div>
<p>However if you incorporate rationals to the exponents, then you will get
well known behavior:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">c</span><span class="p">),</span> <span class="n">x</span><span class="o">**</span><span class="n">c</span><span class="p">)</span>
<span class="go">x**(2*c)*(a + b)</span>
</pre></div>
</div>
<p>Note also that all previously stated facts about <a class="reference internal" href="#sympy.simplify.radsimp.collect" title="sympy.simplify.radsimp.collect"><code class="xref py py-func docutils literal notranslate"><span class="pre">collect()</span></code></a> function
apply to the exponential function, so you can get:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">exp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">),</span> <span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">(a + b)*exp(2*x)</span>
</pre></div>
</div>
<p>If you are interested only in collecting specific powers of some symbols
then set <code class="docutils literal notranslate"><span class="pre">exact</span></code> flag in arguments:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">7</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">7</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">a*x**7 + b*x**7</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">7</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">7</span><span class="p">,</span> <span class="n">x</span><span class="o">**</span><span class="mi">7</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x**7*(a + b)</span>
</pre></div>
</div>
<p>You can also apply this function to differential equations, where
derivatives of arbitrary order can be collected. Note that if you
collect with respect to a function or a derivative of a function, all
derivatives of that function will also be collected. Use
<code class="docutils literal notranslate"><span class="pre">exact=True</span></code> to prevent this from happening:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Derivative</span> <span class="k">as</span> <span class="n">D</span><span class="p">,</span> <span class="n">collect</span><span class="p">,</span> <span class="n">Function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">&#39;f&#39;</span><span class="p">)</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">),</span> <span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">))</span>
<span class="go">(a + b)*Derivative(f(x), x)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">),</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">),</span><span class="n">x</span><span class="p">),</span> <span class="n">f</span><span class="p">)</span>
<span class="go">(a + b)*Derivative(f(x), (x, 2))</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">),</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">),</span><span class="n">x</span><span class="p">),</span> <span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">),</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">a*Derivative(f(x), (x, 2)) + b*Derivative(f(x), (x, 2))</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">a</span><span class="o">*</span><span class="n">f</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">f</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
<span class="go">(a + b)*f(x) + (a + b)*Derivative(f(x), x)</span>
</pre></div>
</div>
<p>Or you can even match both derivative order and exponent at the same time:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">),</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">D</span><span class="p">(</span><span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">),</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">D</span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="n">x</span><span class="p">))</span>
<span class="go">(a + b)*Derivative(f(x), (x, 2))**2</span>
</pre></div>
</div>
<p>Finally, you can apply a function to each of the collected coefficients.
For example you can factorize symbolic coefficients of polynomial:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">expand</span><span class="p">((</span><span class="n">x</span> <span class="o">+</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">collect</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">factor</span><span class="p">)</span>
<span class="go">x**3 + 3*x**2*(a + 1) + 3*x*(a + 1)**2 + (a + 1)**3</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Arguments are expected to be in expanded form, so you might have
to call <a class="reference internal" href="../core.html#sympy.core.function.expand" title="sympy.core.function.expand"><code class="xref py py-func docutils literal notranslate"><span class="pre">expand()</span></code></a> prior to calling this function.</p>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.simplify.radsimp.collect_const" title="sympy.simplify.radsimp.collect_const"><code class="xref py py-obj docutils literal notranslate"><span class="pre">collect_const</span></code></a>, <a class="reference internal" href="#sympy.simplify.radsimp.collect_sqrt" title="sympy.simplify.radsimp.collect_sqrt"><code class="xref py py-obj docutils literal notranslate"><span class="pre">collect_sqrt</span></code></a>, <a class="reference internal" href="#sympy.simplify.radsimp.rcollect" title="sympy.simplify.radsimp.rcollect"><code class="xref py py-obj docutils literal notranslate"><span class="pre">rcollect</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.radsimp.rcollect">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.radsimp.</span></span><span class="sig-name descname"><span class="pre">rcollect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">vars</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/radsimp.py#L454-L482"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.radsimp.rcollect" title="Permalink to this definition">¶</a></dt>
<dd><p>Recursively collect sums in an expression.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify</span> <span class="kn">import</span> <span class="n">rcollect</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">rcollect</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">(x + y*(x**2 + x + 1))/(x + y)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.simplify.radsimp.collect" title="sympy.simplify.radsimp.collect"><code class="xref py py-obj docutils literal notranslate"><span class="pre">collect</span></code></a>, <a class="reference internal" href="#sympy.simplify.radsimp.collect_const" title="sympy.simplify.radsimp.collect_const"><code class="xref py py-obj docutils literal notranslate"><span class="pre">collect_const</span></code></a>, <a class="reference internal" href="#sympy.simplify.radsimp.collect_sqrt" title="sympy.simplify.radsimp.collect_sqrt"><code class="xref py py-obj docutils literal notranslate"><span class="pre">collect_sqrt</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="collect-sqrt">
<h3>collect_sqrt<a class="headerlink" href="#collect-sqrt" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.radsimp.collect_sqrt">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.radsimp.</span></span><span class="sig-name descname"><span class="pre">collect_sqrt</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">evaluate</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/radsimp.py#L485-L561"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.radsimp.collect_sqrt" title="Permalink to this definition">¶</a></dt>
<dd><p>Return expr with terms having common square roots collected together.
If <code class="docutils literal notranslate"><span class="pre">evaluate</span></code> is False a count indicating the number of sqrt-containing
terms will be returned and, if non-zero, the terms of the Add will be
returned, else the expression itself will be returned as a single term.
If <code class="docutils literal notranslate"><span class="pre">evaluate</span></code> is True, the expression with any collected terms will be
returned.</p>
<p>Note: since I = sqrt(-1), it is collected, too.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.radsimp</span> <span class="kn">import</span> <span class="n">collect_sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">r2</span><span class="p">,</span> <span class="n">r3</span><span class="p">,</span> <span class="n">r5</span> <span class="o">=</span> <span class="p">[</span><span class="n">sqrt</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_sqrt</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">r2</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">r2</span><span class="p">)</span>
<span class="go">sqrt(2)*(a + b)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_sqrt</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">r2</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">r2</span> <span class="o">+</span> <span class="n">a</span><span class="o">*</span><span class="n">r3</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">r3</span><span class="p">)</span>
<span class="go">sqrt(2)*(a + b) + sqrt(3)*(a + b)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_sqrt</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">r2</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">r2</span> <span class="o">+</span> <span class="n">a</span><span class="o">*</span><span class="n">r3</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">r5</span><span class="p">)</span>
<span class="go">sqrt(3)*a + sqrt(5)*b + sqrt(2)*(a + b)</span>
</pre></div>
</div>
<p>If evaluate is False then the arguments will be sorted and
returned as a list and a count of the number of sqrt-containing
terms will be returned:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collect_sqrt</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">r2</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">r2</span> <span class="o">+</span> <span class="n">a</span><span class="o">*</span><span class="n">r3</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">r5</span><span class="p">,</span> <span class="n">evaluate</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">((sqrt(3)*a, sqrt(5)*b, sqrt(2)*(a + b)), 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_sqrt</span><span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="p">,</span> <span class="n">evaluate</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">((b, sqrt(2)*a), 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_sqrt</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">,</span> <span class="n">evaluate</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">((a + b,), 0)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.simplify.radsimp.collect" title="sympy.simplify.radsimp.collect"><code class="xref py py-obj docutils literal notranslate"><span class="pre">collect</span></code></a>, <a class="reference internal" href="#sympy.simplify.radsimp.collect_const" title="sympy.simplify.radsimp.collect_const"><code class="xref py py-obj docutils literal notranslate"><span class="pre">collect_const</span></code></a>, <a class="reference internal" href="#sympy.simplify.radsimp.rcollect" title="sympy.simplify.radsimp.rcollect"><code class="xref py py-obj docutils literal notranslate"><span class="pre">rcollect</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="collect-const">
<h3>collect_const<a class="headerlink" href="#collect-const" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.radsimp.collect_const">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.radsimp.</span></span><span class="sig-name descname"><span class="pre">collect_const</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">vars</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">Numbers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/radsimp.py#L614-L747"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.radsimp.collect_const" title="Permalink to this definition">¶</a></dt>
<dd><p>A non-greedy collection of terms with similar number coefficients in
an Add expr. If <code class="docutils literal notranslate"><span class="pre">vars</span></code> is given then only those constants will be
targeted. Although any Number can also be targeted, if this is not
desired set <code class="docutils literal notranslate"><span class="pre">Numbers=False</span></code> and no Float or Rational will be collected.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>expr</strong> : sympy expression</p>
<blockquote>
<div><p>This parameter defines the expression the expression from which
terms with similar coefficients are to be collected. A non-Add
expression is returned as it is.</p>
</div></blockquote>
<p><strong>vars</strong> : variable length collection of Numbers, optional</p>
<blockquote>
<div><p>Specifies the constants to target for collection. Can be multiple in
number.</p>
</div></blockquote>
<p><strong>Numbers</strong> : bool</p>
<blockquote>
<div><p>Specifies to target all instance of
<a class="reference internal" href="../core.html#sympy.core.numbers.Number" title="sympy.core.numbers.Number"><code class="xref py py-class docutils literal notranslate"><span class="pre">sympy.core.numbers.Number</span></code></a> class. If <code class="docutils literal notranslate"><span class="pre">Numbers=False</span></code>, then
no Float or Rational will be collected.</p>
</div></blockquote>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><strong>expr</strong> : Expr</p>
<blockquote>
<div><p>Returns an expression with similar coefficient terms collected.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">s</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.radsimp</span> <span class="kn">import</span> <span class="n">collect_const</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_const</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)))</span>
<span class="go">sqrt(3)*(sqrt(2) + 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_const</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="n">s</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="o">*</span><span class="n">s</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">7</span><span class="p">))</span>
<span class="go">(sqrt(3) + sqrt(7))*(s + 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_const</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="n">s</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="o">*</span><span class="n">s</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">7</span><span class="p">))</span>
<span class="go">(sqrt(2) + 3)*(sqrt(3) + sqrt(7))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_const</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="n">s</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="o">*</span><span class="n">s</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">7</span><span class="p">),</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="go">sqrt(7) + sqrt(3)*(sqrt(2) + 3) + sqrt(7)*(sqrt(2) + 2)</span>
</pre></div>
</div>
<p>The collection is sign-sensitive, giving higher precedence to the
unsigned values:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collect_const</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">y</span> <span class="o">-</span> <span class="n">z</span><span class="p">)</span>
<span class="go">x - (y + z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_const</span><span class="p">(</span><span class="o">-</span><span class="n">y</span> <span class="o">-</span> <span class="n">z</span><span class="p">)</span>
<span class="go">-(y + z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_const</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">z</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">2*(x - y - z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collect_const</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">z</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">)</span>
<span class="go">2*x - 2*(y + z)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.simplify.radsimp.collect" title="sympy.simplify.radsimp.collect"><code class="xref py py-obj docutils literal notranslate"><span class="pre">collect</span></code></a>, <a class="reference internal" href="#sympy.simplify.radsimp.collect_sqrt" title="sympy.simplify.radsimp.collect_sqrt"><code class="xref py py-obj docutils literal notranslate"><span class="pre">collect_sqrt</span></code></a>, <a class="reference internal" href="#sympy.simplify.radsimp.rcollect" title="sympy.simplify.radsimp.rcollect"><code class="xref py py-obj docutils literal notranslate"><span class="pre">rcollect</span></code></a></p>
</div>
</dd></dl>

</section>
<section id="fraction">
<h3>fraction<a class="headerlink" href="#fraction" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.radsimp.fraction">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.radsimp.</span></span><span class="sig-name descname"><span class="pre">fraction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exact</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/radsimp.py#L1025-L1113"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.radsimp.fraction" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a pair with expression’s numerator and denominator.
If the given expression is not a fraction then this function
will return the tuple (expr, 1).</p>
<p>This function will not make any attempt to simplify nested
fractions or to do any term rewriting at all.</p>
<p>If only one of the numerator/denominator pair is needed then
use numer(expr) or denom(expr) functions respectively.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">fraction</span><span class="p">,</span> <span class="n">Rational</span><span class="p">,</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="n">x</span><span class="o">/</span><span class="n">y</span><span class="p">)</span>
<span class="go">(x, y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">(x, 1)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(1, y**2)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(x*y, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="go">(1, 2)</span>
</pre></div>
</div>
<p>This function will also work fine with assumptions:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">k</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;k&#39;</span><span class="p">,</span> <span class="n">negative</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">y</span><span class="o">**</span><span class="n">k</span><span class="p">)</span>
<span class="go">(x, y**(-k))</span>
</pre></div>
</div>
<p>If we know nothing about sign of some exponent and <code class="docutils literal notranslate"><span class="pre">exact</span></code>
flag is unset, then structure this exponent’s structure will
be analyzed and pretty fraction will be returned:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">exp</span><span class="p">,</span> <span class="n">Mul</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="o">-</span><span class="n">y</span><span class="p">))</span>
<span class="go">(2, x**y)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">))</span>
<span class="go">(1, exp(x))</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">),</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(exp(-x), 1)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">exact</span></code> flag will also keep any unevaluated Muls from
being evaluated:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">u</span> <span class="o">=</span> <span class="n">Mul</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">evaluate</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="n">u</span><span class="p">)</span>
<span class="go">(2*x + 2, 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fraction</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(2*(x  + 1), 1)</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
<section id="module-sympy.simplify.ratsimp">
<span id="ratsimp"></span><h2>Ratsimp<a class="headerlink" href="#module-sympy.simplify.ratsimp" title="Permalink to this headline">¶</a></h2>
<section id="id3">
<h3>ratsimp<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.ratsimp.ratsimp">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.ratsimp.</span></span><span class="sig-name descname"><span class="pre">ratsimp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/ratsimp.py#L9-L28"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.ratsimp.ratsimp" title="Permalink to this definition">¶</a></dt>
<dd><p>Put an expression over a common denominator, cancel and reduce.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">ratsimp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ratsimp</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="o">/</span><span class="n">y</span><span class="p">)</span>
<span class="go">(x + y)/(x*y)</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="ratsimpmodprime">
<h3>ratsimpmodprime<a class="headerlink" href="#ratsimpmodprime" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.ratsimp.ratsimpmodprime">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.ratsimp.</span></span><span class="sig-name descname"><span class="pre">ratsimpmodprime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">G</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">gens</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">quick</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">polynomial</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/ratsimp.py#L31-L221"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.ratsimp.ratsimpmodprime" title="Permalink to this definition">¶</a></dt>
<dd><p>Simplifies a rational expression <code class="docutils literal notranslate"><span class="pre">expr</span></code> modulo the prime ideal
generated by <code class="docutils literal notranslate"><span class="pre">G</span></code>.  <code class="docutils literal notranslate"><span class="pre">G</span></code> should be a Groebner basis of the
ideal.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.ratsimp</span> <span class="kn">import</span> <span class="n">ratsimpmodprime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">5</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ratsimpmodprime</span><span class="p">(</span><span class="n">eq</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">5</span> <span class="o">-</span> <span class="n">x</span> <span class="o">-</span> <span class="n">y</span><span class="p">],</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s1">&#39;lex&#39;</span><span class="p">)</span>
<span class="go">(-x**2 - x*y - x - y)/(-x**2 + x*y)</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">polynomial</span></code> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, the algorithm computes a rational
simplification which minimizes the sum of the total degrees of
the numerator and the denominator.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">polynomial</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, this function just brings numerator and
denominator into a canonical form. This is much faster, but has
potentially worse results.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r706"><span class="brackets"><a class="fn-backref" href="#id4">R706</a></span></dt>
<dd><p>M. Monagan, R. Pearce, Rational Simplification Modulo a Polynomial
Ideal, <a class="reference external" href="http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.163.6984">http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.163.6984</a>
(specifically, the second algorithm)</p>
</dd>
</dl>
</dd></dl>

</section>
</section>
<section id="module-sympy.simplify.trigsimp">
<span id="trigonometric-simplification"></span><h2>Trigonometric simplification<a class="headerlink" href="#module-sympy.simplify.trigsimp" title="Permalink to this headline">¶</a></h2>
<section id="trigsimp">
<h3>trigsimp<a class="headerlink" href="#trigsimp" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.trigsimp.trigsimp">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.trigsimp.</span></span><span class="sig-name descname"><span class="pre">trigsimp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">opts</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/trigsimp.py#L426-L510"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.trigsimp.trigsimp" title="Permalink to this definition">¶</a></dt>
<dd><p>reduces expression by using known trig identities</p>
<p class="rubric">Explanation</p>
<p>method:
- Determine the method to use. Valid choices are ‘matching’ (default),
‘groebner’, ‘combined’, and ‘fu’. If ‘matching’, simplify the
expression recursively by targeting common patterns. If ‘groebner’, apply
an experimental groebner basis algorithm. In this case further options
are forwarded to <code class="docutils literal notranslate"><span class="pre">trigsimp_groebner</span></code>, please refer to its docstring.
If ‘combined’, first run the groebner basis algorithm with small
default parameters, then run the ‘matching’ algorithm. ‘fu’ runs the
collection of trigonometric transformations described by Fu, et al.
(see the <span class="math notranslate nohighlight">\(fu\)</span> docstring).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">trigsimp</span><span class="p">,</span> <span class="n">sin</span><span class="p">,</span> <span class="n">cos</span><span class="p">,</span> <span class="n">log</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">e</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="go">2</span>
</pre></div>
</div>
<p>Simplification occurs wherever trigonometric functions are located.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
<span class="go">log(2)</span>
</pre></div>
</div>
<p>Using <span class="math notranslate nohighlight">\(method=&quot;groebner&quot;\)</span> (or <span class="math notranslate nohighlight">\(&quot;combined&quot;\)</span>) might lead to greater
simplification.</p>
<p>The old trigsimp routine can be accessed as with method ‘old’.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">coth</span><span class="p">,</span> <span class="n">tanh</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="n">tanh</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">7</span> <span class="o">-</span> <span class="mi">2</span><span class="o">/</span><span class="n">coth</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">7</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;old&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">t</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="go">tanh(x)**7</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
<section id="module-sympy.simplify.powsimp">
<span id="power-simplification"></span><h2>Power simplification<a class="headerlink" href="#module-sympy.simplify.powsimp" title="Permalink to this headline">¶</a></h2>
<section id="powsimp">
<h3>powsimp<a class="headerlink" href="#powsimp" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.powsimp.powsimp">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.powsimp.</span></span><span class="sig-name descname"><span class="pre">powsimp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">expr</span></em>, <em class="sig-param"><span class="pre">deep=False</span></em>, <em class="sig-param"><span class="pre">combine='all'</span></em>, <em class="sig-param"><span class="pre">force=False</span></em>, <em class="sig-param"><span class="pre">measure=&lt;function</span> <span class="pre">count_ops&gt;</span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/powsimp.py#L15-L484"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.powsimp.powsimp" title="Permalink to this definition">¶</a></dt>
<dd><p>reduces expression by combining powers with similar bases and exponents.</p>
<p class="rubric">Explanation</p>
<p>If <code class="docutils literal notranslate"><span class="pre">deep</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then powsimp() will also simplify arguments of
functions. By default <code class="docutils literal notranslate"><span class="pre">deep</span></code> is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">force</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then bases will be combined without checking for
assumptions, e.g. sqrt(x)*sqrt(y) -&gt; sqrt(x*y) which is not true
if x and y are both negative.</p>
<p>You can make powsimp() only combine bases or only combine exponents by
changing combine=’base’ or combine=’exp’.  By default, combine=’all’,
which does both.  combine=’base’ will only combine:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span>   <span class="n">a</span>          <span class="n">a</span>                          <span class="mi">2</span><span class="n">x</span>      <span class="n">x</span>
<span class="n">x</span> <span class="o">*</span> <span class="n">y</span>  <span class="o">=&gt;</span>  <span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span>   <span class="k">as</span> <span class="n">well</span> <span class="k">as</span> <span class="n">things</span> <span class="n">like</span> <span class="mi">2</span>   <span class="o">=&gt;</span>  <span class="mi">4</span>
</pre></div>
</div>
<p>and combine=’exp’ will only combine</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span>   <span class="n">b</span>      <span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span>
<span class="n">x</span> <span class="o">*</span> <span class="n">x</span>  <span class="o">=&gt;</span>  <span class="n">x</span>
</pre></div>
</div>
<p>combine=’exp’ will strictly only combine exponents in the way that used
to be automatic.  Also use deep=True if you need the old behavior.</p>
<p>When combine=’all’, ‘exp’ is evaluated first.  Consider the first
example below for when there could be an ambiguity relating to this.
This is done so things like the second example can be completely
combined.  If you want ‘base’ combined first, do something like
powsimp(powsimp(expr, combine=’base’), combine=’exp’).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">powsimp</span><span class="p">,</span> <span class="n">exp</span><span class="p">,</span> <span class="n">log</span><span class="p">,</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">n</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;all&#39;</span><span class="p">)</span>
<span class="go">x**(y + z)*y**z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;exp&#39;</span><span class="p">)</span>
<span class="go">x**(y + z)*y**z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;base&#39;</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x**y*(x*y)**z</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;all&#39;</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(n*x)**(y + z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;exp&#39;</span><span class="p">)</span>
<span class="go">n**(y + z)*x**(y + z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">&#39;base&#39;</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(n*x)**y*(n*x)**z</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="n">y</span><span class="p">)))</span>
<span class="go">log(exp(x)*exp(y))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="n">y</span><span class="p">)),</span> <span class="n">deep</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x + y</span>
</pre></div>
</div>
<p>Radicals with Mul bases will be combined if combine=’exp’</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Two radicals are automatically joined through Mul:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">**</span><span class="mi">3</span> <span class="o">==</span> <span class="n">a</span><span class="o">**</span><span class="mi">4</span>
<span class="go">True</span>
</pre></div>
</div>
<p>But if an integer power of that radical has been
autoexpanded then Mul does not join the resulting factors:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">**</span><span class="mi">4</span> <span class="c1"># auto expands to a Mul, no longer a Pow</span>
<span class="go">x**2*y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span><span class="o">*</span><span class="n">a</span> <span class="c1"># so Mul doesn&#39;t combine them</span>
<span class="go">x**2*y*sqrt(x*sqrt(y))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">_</span><span class="p">)</span> <span class="c1"># but powsimp will</span>
<span class="go">(x*sqrt(y))**(5/2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">a</span><span class="p">)</span> <span class="c1"># but won&#39;t when doing so would violate assumptions</span>
<span class="go">x*y*sqrt(x*sqrt(y))</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="powdenest">
<h3>powdenest<a class="headerlink" href="#powdenest" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.powsimp.powdenest">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.powsimp.</span></span><span class="sig-name descname"><span class="pre">powdenest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">eq</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">force</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">polar</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/powsimp.py#L487-L602"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.powsimp.powdenest" title="Permalink to this definition">¶</a></dt>
<dd><p>Collect exponents on powers as assumptions allow.</p>
<p class="rubric">Explanation</p>
<dl class="simple">
<dt>Given <code class="docutils literal notranslate"><span class="pre">(bb**be)**e</span></code>, this can be simplified as follows:</dt><dd><ul class="simple">
<li><p>if <code class="docutils literal notranslate"><span class="pre">bb</span></code> is positive, or</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">e</span></code> is an integer, or</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">|be|</span> <span class="pre">&lt;</span> <span class="pre">1</span></code> then this simplifies to <code class="docutils literal notranslate"><span class="pre">bb**(be*e)</span></code></p></li>
</ul>
</dd>
</dl>
<p>Given a product of powers raised to a power, <code class="docutils literal notranslate"><span class="pre">(bb1**be1</span> <span class="pre">*</span>
<span class="pre">bb2**be2...)**e</span></code>, simplification can be done as follows:</p>
<ul class="simple">
<li><p>if e is positive, the gcd of all bei can be joined with e;</p></li>
<li><p>all non-negative bb can be separated from those that are negative
and their gcd can be joined with e; autosimplification already
handles this separation.</p></li>
<li><p>integer factors from powers that have integers in the denominator
of the exponent can be removed from any term and the gcd of such
integers can be joined with e</p></li>
</ul>
<p>Setting <code class="docutils literal notranslate"><span class="pre">force</span></code> to <code class="docutils literal notranslate"><span class="pre">True</span></code> will make symbols that are not explicitly
negative behave as though they are positive, resulting in more
denesting.</p>
<p>Setting <code class="docutils literal notranslate"><span class="pre">polar</span></code> to <code class="docutils literal notranslate"><span class="pre">True</span></code> will do simplifications on the Riemann surface of
the logarithm, also resulting in more denestings.</p>
<p>When there are sums of logs in exp() then a product of powers may be
obtained e.g. <code class="docutils literal notranslate"><span class="pre">exp(3*(log(a)</span> <span class="pre">+</span> <span class="pre">2*log(b)))</span></code> - &gt; <code class="docutils literal notranslate"><span class="pre">a**3*b**6</span></code>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">exp</span><span class="p">,</span> <span class="n">log</span><span class="p">,</span> <span class="n">sqrt</span><span class="p">,</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">powdenest</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">a</span><span class="o">/</span><span class="mi">3</span><span class="p">))</span><span class="o">**</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="p">))</span>
<span class="go">(x**(2*a/3))**(3*x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span><span class="p">)))</span>
<span class="go">2**(3*x)</span>
</pre></div>
</div>
<p>Assumptions may prevent expansion:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="go">sqrt(x**2)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;p&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">p</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="go">p</span>
</pre></div>
</div>
<p>No other expansion is done.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;i,j&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span><span class="p">))</span> <span class="c1"># -X-&gt; (x**x)**i*(x**x)**j</span>
<span class="go">x**(x*(i + j))</span>
</pre></div>
</div>
<p>But exp() will be denested by moving all non-log terms outside of
the function; this may result in the collapsing of the exp to a power
with a different base:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>
<span class="go">x**(3*y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">y</span><span class="o">*</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">+</span> <span class="n">log</span><span class="p">(</span><span class="n">b</span><span class="p">))))</span>
<span class="go">(a*b)**y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">+</span> <span class="n">log</span><span class="p">(</span><span class="n">b</span><span class="p">))))</span>
<span class="go">a**3*b**3</span>
</pre></div>
</div>
<p>If assumptions allow, symbols can also be moved to the outermost exponent:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;i&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">(((</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span><span class="p">))</span><span class="o">**</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="p">))</span><span class="o">**</span><span class="n">x</span><span class="p">)</span>
<span class="go">((x**(2*i))**(3*y))**x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">(((</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span><span class="p">))</span><span class="o">**</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="p">))</span><span class="o">**</span><span class="n">x</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x**(6*i*x*y)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">(((</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">a</span><span class="o">/</span><span class="mi">3</span><span class="p">))</span><span class="o">**</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="o">/</span><span class="n">i</span><span class="p">))</span><span class="o">**</span><span class="n">x</span><span class="p">)</span>
<span class="go">((x**(2*a/3))**(3*y/i))**x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span><span class="p">)</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">i</span><span class="p">))</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(x*y**2)**(2*i*z)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">,</span> <span class="n">negative</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="n">i</span><span class="p">)</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x**(i*y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">((</span><span class="n">n</span><span class="o">**</span><span class="n">i</span><span class="p">)</span><span class="o">**</span><span class="n">x</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(n**i)**x</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
<section id="module-sympy.simplify.combsimp">
<span id="combinatorial-simplification"></span><h2>Combinatorial simplification<a class="headerlink" href="#module-sympy.simplify.combsimp" title="Permalink to this headline">¶</a></h2>
<section id="combsimp">
<h3>combsimp<a class="headerlink" href="#combsimp" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.combsimp.combsimp">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.combsimp.</span></span><span class="sig-name descname"><span class="pre">combsimp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/combsimp.py#L11-L57"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.combsimp.combsimp" title="Permalink to this definition">¶</a></dt>
<dd><p>Simplify combinatorial expressions.</p>
<p class="rubric">Explanation</p>
<p>This function takes as input an expression containing factorials,
binomials, Pochhammer symbol and other “combinatorial” functions,
and tries to minimize the number of those functions and reduce
the size of their arguments.</p>
<p>The algorithm works by rewriting all combinatorial functions as
gamma functions and applying gammasimp() except simplification
steps that may make an integer argument non-integer. See docstring
of gammasimp for more information.</p>
<p>Then it rewrites expression in terms of factorials and binomials by
rewriting gammas as factorials and converting (a+b)!/a!b! into
binomials.</p>
<p>If expression has gamma functions or combinatorial functions
with non-integer argument, it is automatically passed to gammasimp.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify</span> <span class="kn">import</span> <span class="n">combsimp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">factorial</span><span class="p">,</span> <span class="n">binomial</span><span class="p">,</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="p">,</span> <span class="n">k</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;n k&#39;</span><span class="p">,</span> <span class="n">integer</span> <span class="o">=</span> <span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">combsimp</span><span class="p">(</span><span class="n">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">/</span><span class="n">factorial</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">3</span><span class="p">))</span>
<span class="go">n*(n - 2)*(n - 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">combsimp</span><span class="p">(</span><span class="n">binomial</span><span class="p">(</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">binomial</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
<span class="go">(n + 1)/(k + 1)</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
<section id="module-sympy.simplify.sqrtdenest">
<span id="square-root-denesting"></span><h2>Square Root Denesting<a class="headerlink" href="#module-sympy.simplify.sqrtdenest" title="Permalink to this headline">¶</a></h2>
<section id="sqrtdenest">
<h3>sqrtdenest<a class="headerlink" href="#sqrtdenest" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.sqrtdenest.sqrtdenest">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.sqrtdenest.</span></span><span class="sig-name descname"><span class="pre">sqrtdenest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_iter</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">3</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/sqrtdenest.py#L102-L135"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.sqrtdenest.sqrtdenest" title="Permalink to this definition">¶</a></dt>
<dd><p>Denests sqrts in an expression that contain other square roots
if possible, otherwise returns the expr unchanged. This is based on the
algorithms of [1].</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.sqrtdenest</span> <span class="kn">import</span> <span class="n">sqrtdenest</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrtdenest</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">6</span><span class="p">)))</span>
<span class="go">sqrt(2) + sqrt(3)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../solvers/solvers.html#sympy.solvers.solvers.unrad" title="sympy.solvers.solvers.unrad"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.solvers.solvers.unrad</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r707"><span class="brackets"><a class="fn-backref" href="#id5">R707</a></span></dt>
<dd><p><a class="reference external" href="http://researcher.watson.ibm.com/researcher/files/us-fagin/symb85.pdf">http://researcher.watson.ibm.com/researcher/files/us-fagin/symb85.pdf</a></p>
</dd>
<dt class="label" id="r708"><span class="brackets"><a class="fn-backref" href="#id6">R708</a></span></dt>
<dd><p>D. J. Jeffrey and A. D. Rich, ‘Symplifying Square Roots of Square Roots
by Denesting’ (available at <a class="reference external" href="http://www.cybertester.com/data/denest.pdf">http://www.cybertester.com/data/denest.pdf</a>)</p>
</dd>
</dl>
</dd></dl>

</section>
</section>
<section id="module-sympy.simplify.cse_main">
<span id="common-subexpression-elimination"></span><h2>Common Subexpression Elimination<a class="headerlink" href="#module-sympy.simplify.cse_main" title="Permalink to this headline">¶</a></h2>
<section id="cse">
<h3>cse<a class="headerlink" href="#cse" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.cse_main.cse">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.cse_main.</span></span><span class="sig-name descname"><span class="pre">cse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exprs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symbols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optimizations</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">postprocess</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'canonical'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignore</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">list</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/cse_main.py#L697-L855"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.cse_main.cse" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform common subexpression elimination on an expression.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>exprs</strong> : list of sympy expressions, or a single sympy expression</p>
<blockquote>
<div><p>The expressions to reduce.</p>
</div></blockquote>
<p><strong>symbols</strong> : infinite iterator yielding unique Symbols</p>
<blockquote>
<div><p>The symbols used to label the common subexpressions which are pulled
out. The <code class="docutils literal notranslate"><span class="pre">numbered_symbols</span></code> generator is useful. The default is a
stream of symbols of the form “x0”, “x1”, etc. This must be an
infinite iterator.</p>
</div></blockquote>
<p><strong>optimizations</strong> : list of (callable, callable) pairs</p>
<blockquote>
<div><p>The (preprocessor, postprocessor) pairs of external optimization
functions. Optionally ‘basic’ can be passed for a set of predefined
basic optimizations. Such ‘basic’ optimizations were used by default
in old implementation, however they can be really slow on larger
expressions. Now, no pre or post optimizations are made by default.</p>
</div></blockquote>
<p><strong>postprocess</strong> : a function which accepts the two return values of cse and</p>
<blockquote>
<div><p>returns the desired form of output from cse, e.g. if you want the
replacements reversed the function might be the following lambda:
lambda r, e: return reversed(r), e</p>
</div></blockquote>
<p><strong>order</strong> : string, ‘none’ or ‘canonical’</p>
<blockquote>
<div><p>The order by which Mul and Add arguments are processed. If set to
‘canonical’, arguments will be canonically ordered. If set to ‘none’,
ordering will be faster but dependent on expressions hashes, thus
machine dependent and variable. For large expressions where speed is a
concern, use the setting order=’none’.</p>
</div></blockquote>
<p><strong>ignore</strong> : iterable of Symbols</p>
<blockquote>
<div><p>Substitutions containing any Symbol from <code class="docutils literal notranslate"><span class="pre">ignore</span></code> will be ignored.</p>
</div></blockquote>
<p><strong>list</strong> : bool, (default True)</p>
<blockquote>
<div><p>Returns expression in list or else with same type as input (when False).</p>
</div></blockquote>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><strong>replacements</strong> : list of (Symbol, expression) pairs</p>
<blockquote>
<div><p>All of the common subexpressions that were replaced. Subexpressions
earlier in this list might show up in subexpressions later in this
list.</p>
</div></blockquote>
<p><strong>reduced_exprs</strong> : list of sympy expressions</p>
<blockquote>
<div><p>The reduced expressions with all of the replacements above.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">cse</span><span class="p">,</span> <span class="n">SparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">w</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cse</span><span class="p">(((</span><span class="n">w</span> <span class="o">+</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">w</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="n">w</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span>
<span class="go">([(x0, y + z), (x1, w + x)], [(w + x0)*(x0 + x1)/x1**3])</span>
</pre></div>
</div>
<p>Note that currently, y + z will not get substituted if -y - z is used.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cse</span><span class="p">(((</span><span class="n">w</span> <span class="o">+</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">w</span> <span class="o">-</span> <span class="n">y</span> <span class="o">-</span> <span class="n">z</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="n">w</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span>
<span class="go"> ([(x0, w + x)], [(w - y - z)*(x0 + y + z)/x0**3])</span>
</pre></div>
</div>
<p>List of expressions with recursive substitutions:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">([</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cse</span><span class="p">([(</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">,</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="n">z</span> <span class="o">+</span> <span class="n">y</span><span class="p">,</span> <span class="n">m</span><span class="p">])</span>
<span class="go">([(x0, x + y), (x1, x0 + z)], [x0**2, x1, y + z, x1, Matrix([</span>
<span class="go">[x0],</span>
<span class="go">[x1]])])</span>
</pre></div>
</div>
<p>Note: the type and mutability of input matrices is retained.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">_</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">SparseMatrix</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The user may disallow substitutions containing certain symbols:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cse</span><span class="p">([</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">3</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">ignore</span><span class="o">=</span><span class="p">(</span><span class="n">y</span><span class="p">,))</span>
<span class="go">([(x0, x + 1)], [x0*y**2, 3*x0*y**2])</span>
</pre></div>
</div>
<p>The default return value for the reduced expression(s) is a list, even if there is only
one expression. The <span class="math notranslate nohighlight">\(list\)</span> flag preserves the type of the input in the output:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cse</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">([], [x])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cse</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">list</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">([], x)</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="opt-cse">
<h3>opt_cse<a class="headerlink" href="#opt-cse" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.cse_main.opt_cse">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.cse_main.</span></span><span class="sig-name descname"><span class="pre">opt_cse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exprs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'canonical'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/cse_main.py#L454-L549"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.cse_main.opt_cse" title="Permalink to this definition">¶</a></dt>
<dd><p>Find optimization opportunities in Adds, Muls, Pows and negative
coefficient Muls.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>exprs</strong> : list of sympy expressions</p>
<blockquote>
<div><p>The expressions to optimize.</p>
</div></blockquote>
<p><strong>order</strong> : string, ‘none’ or ‘canonical’</p>
<blockquote>
<div><p>The order by which Mul and Add arguments are processed. For large
expressions where speed is a concern, use the setting order=’none’.</p>
</div></blockquote>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><strong>opt_subs</strong> : dictionary of expression substitutions</p>
<blockquote>
<div><p>The expression substitutions which can be useful to optimize CSE.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.cse_main</span> <span class="kn">import</span> <span class="n">opt_cse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">opt_subs</span> <span class="o">=</span> <span class="n">opt_cse</span><span class="p">([</span><span class="n">x</span><span class="o">**-</span><span class="mi">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">opt_subs</span><span class="o">.</span><span class="n">keys</span><span class="p">())[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">list</span><span class="p">(</span><span class="n">opt_subs</span><span class="o">.</span><span class="n">values</span><span class="p">())[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">.</span><span class="n">as_unevaluated_basic</span><span class="p">()))</span>
<span class="go">(x**(-2), 1/(x**2))</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="tree-cse">
<h3>tree_cse<a class="headerlink" href="#tree-cse" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.cse_main.tree_cse">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.cse_main.</span></span><span class="sig-name descname"><span class="pre">tree_cse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exprs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symbols</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">opt_subs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'canonical'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignore</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/cse_main.py#L552-L694"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.cse_main.tree_cse" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform raw CSE on expression tree, taking opt_subs into account.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>exprs</strong> : list of sympy expressions</p>
<blockquote>
<div><p>The expressions to reduce.</p>
</div></blockquote>
<p><strong>symbols</strong> : infinite iterator yielding unique Symbols</p>
<blockquote>
<div><p>The symbols used to label the common subexpressions which are pulled
out.</p>
</div></blockquote>
<p><strong>opt_subs</strong> : dictionary of expression substitutions</p>
<blockquote>
<div><p>The expressions to be substituted before any CSE action is performed.</p>
</div></blockquote>
<p><strong>order</strong> : string, ‘none’ or ‘canonical’</p>
<blockquote>
<div><p>The order by which Mul and Add arguments are processed. For large
expressions where speed is a concern, use the setting order=’none’.</p>
</div></blockquote>
<p><strong>ignore</strong> : iterable of Symbols</p>
<blockquote>
<div><p>Substitutions containing any Symbol from <code class="docutils literal notranslate"><span class="pre">ignore</span></code> will be ignored.</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

</section>
</section>
<section id="module-sympy.simplify.hyperexpand">
<span id="hypergeometric-function-expansion"></span><h2>Hypergeometric Function Expansion<a class="headerlink" href="#module-sympy.simplify.hyperexpand" title="Permalink to this headline">¶</a></h2>
<section id="hyperexpand">
<h3>hyperexpand<a class="headerlink" href="#hyperexpand" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.hyperexpand.hyperexpand">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.hyperexpand.</span></span><span class="sig-name descname"><span class="pre">hyperexpand</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">allow_hyper</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rewrite</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'default'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">place</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/hyperexpand.py#L2474-L2513"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.hyperexpand.hyperexpand" title="Permalink to this definition">¶</a></dt>
<dd><p>Expand hypergeometric functions. If allow_hyper is True, allow partial
simplification (that is a result different from input,
but still containing hypergeometric functions).</p>
<p>If a G-function has expansions both at zero and at infinity,
<code class="docutils literal notranslate"><span class="pre">place</span></code> can be set to <code class="docutils literal notranslate"><span class="pre">0</span></code> or <code class="docutils literal notranslate"><span class="pre">zoo</span></code> to indicate the
preferred choice.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.hyperexpand</span> <span class="kn">import</span> <span class="n">hyperexpand</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.functions</span> <span class="kn">import</span> <span class="n">hyper</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">hyperexpand</span><span class="p">(</span><span class="n">hyper</span><span class="p">([],</span> <span class="p">[],</span> <span class="n">z</span><span class="p">))</span>
<span class="go">exp(z)</span>
</pre></div>
</div>
<p>Non-hyperegeometric parts of the expression and hypergeometric expressions
that are not recognised are left unchanged:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">hyperexpand</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">hyper</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[],</span> <span class="n">z</span><span class="p">))</span>
<span class="go">hyper((1, 1, 1), (), z) + 1</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
<section id="module-sympy.simplify.traversaltools">
<span id="traversal-tools"></span><h2>Traversal Tools<a class="headerlink" href="#module-sympy.simplify.traversaltools" title="Permalink to this headline">¶</a></h2>
<section id="use">
<h3>use<a class="headerlink" href="#use" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.traversaltools.use">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.traversaltools.</span></span><span class="sig-name descname"><span class="pre">use</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/traversaltools.py#L6-L39"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.traversaltools.use" title="Permalink to this definition">¶</a></dt>
<dd><p>Use <code class="docutils literal notranslate"><span class="pre">func</span></code> to transform <code class="docutils literal notranslate"><span class="pre">expr</span></code> at the given level.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">use</span><span class="p">,</span> <span class="n">expand</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">use</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">expand</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="go">x*(x**2 + 2*x*y + y**2) + 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">x**3 + 2*x**2*y + x*y**2 + 1</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
<section id="module-sympy.simplify.epathtools">
<span id="epath-tools"></span><h2>EPath Tools<a class="headerlink" href="#module-sympy.simplify.epathtools" title="Permalink to this headline">¶</a></h2>
<section id="epath-class">
<h3>EPath class<a class="headerlink" href="#epath-class" title="Permalink to this headline">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.simplify.epathtools.EPath">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.simplify.epathtools.</span></span><span class="sig-name descname"><span class="pre">EPath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/epathtools.py#L6-L278"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.epathtools.EPath" title="Permalink to this definition">¶</a></dt>
<dd><p>Manipulate expressions using paths.</p>
<p>EPath grammar in EBNF notation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>literal   ::= /[A-Za-z_][A-Za-z_0-9]*/
number    ::= /-?\d+/
type      ::= literal
attribute ::= literal &quot;?&quot;
all       ::= &quot;*&quot;
slice     ::= &quot;[&quot; number? (&quot;:&quot; number? (&quot;:&quot; number?)?)? &quot;]&quot;
range     ::= all | slice
query     ::= (type | attribute) (&quot;|&quot; (type | attribute))*
selector  ::= range | query range?
path      ::= &quot;/&quot; selector (&quot;/&quot; selector)*
</pre></div>
</div>
<p>See the docstring of the epath() function.</p>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.simplify.epathtools.EPath.apply">
<span class="sig-name descname"><span class="pre">apply</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/epathtools.py#L154-L222"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.epathtools.EPath.apply" title="Permalink to this definition">¶</a></dt>
<dd><p>Modify parts of an expression selected by a path.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.epathtools</span> <span class="kn">import</span> <span class="n">EPath</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sin</span><span class="p">,</span> <span class="n">cos</span><span class="p">,</span> <span class="n">E</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">t</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span> <span class="o">=</span> <span class="n">EPath</span><span class="p">(</span><span class="s2">&quot;/*/[0]/Symbol&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">[((</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">2</span><span class="p">),</span> <span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="n">z</span><span class="p">)]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">expr</span><span class="p">:</span> <span class="n">expr</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">[((x**2, 1), 2), ((3, y**2), z)]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span> <span class="o">=</span> <span class="n">EPath</span><span class="p">(</span><span class="s2">&quot;/*/*/Symbol&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">t</span> <span class="o">+</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">E</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">expr</span><span class="p">:</span> <span class="mi">2</span><span class="o">*</span><span class="n">expr</span><span class="p">)</span>
<span class="go">t + sin(2*x + 1) + cos(2*x + 2*y + E)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.simplify.epathtools.EPath.select">
<span class="sig-name descname"><span class="pre">select</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/epathtools.py#L224-L278"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.epathtools.EPath.select" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve parts of an expression selected by a path.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.epathtools</span> <span class="kn">import</span> <span class="n">EPath</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sin</span><span class="p">,</span> <span class="n">cos</span><span class="p">,</span> <span class="n">E</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">t</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span> <span class="o">=</span> <span class="n">EPath</span><span class="p">(</span><span class="s2">&quot;/*/[0]/Symbol&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">[((</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">2</span><span class="p">),</span> <span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="n">z</span><span class="p">)]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">[x, y]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span> <span class="o">=</span> <span class="n">EPath</span><span class="p">(</span><span class="s2">&quot;/*/*/Symbol&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">t</span> <span class="o">+</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">E</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">[x, x, y]</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

</section>
<section id="epath">
<h3>epath<a class="headerlink" href="#epath" title="Permalink to this headline">¶</a></h3>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.simplify.epathtools.epath">
<span class="sig-prename descclassname"><span class="pre">sympy.simplify.epathtools.</span></span><span class="sig-name descname"><span class="pre">epath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">expr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/simplify/epathtools.py#L281-L356"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.simplify.epathtools.epath" title="Permalink to this definition">¶</a></dt>
<dd><p>Manipulate parts of an expression selected by a path.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>path</strong> : str | EPath</p>
<blockquote>
<div><p>A path as a string or a compiled EPath.</p>
</div></blockquote>
<p><strong>expr</strong> : Basic | iterable</p>
<blockquote>
<div><p>An expression or a container of expressions.</p>
</div></blockquote>
<p><strong>func</strong> : callable (optional)</p>
<blockquote>
<div><p>A callable that will be applied to matching parts.</p>
</div></blockquote>
<p><strong>args</strong> : tuple (optional)</p>
<blockquote>
<div><p>Additional positional arguments to <code class="docutils literal notranslate"><span class="pre">func</span></code>.</p>
</div></blockquote>
<p><strong>kwargs</strong> : dict (optional)</p>
<blockquote>
<div><p>Additional keyword arguments to <code class="docutils literal notranslate"><span class="pre">func</span></code>.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>This function allows to manipulate large nested expressions in single
line of code, utilizing techniques to those applied in XML processing
standards (e.g. XPath).</p>
<p>If <code class="docutils literal notranslate"><span class="pre">func</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#sympy.simplify.epathtools.epath" title="sympy.simplify.epathtools.epath"><code class="xref py py-func docutils literal notranslate"><span class="pre">epath()</span></code></a> retrieves elements selected by
the <code class="docutils literal notranslate"><span class="pre">path</span></code>. Otherwise it applies <code class="docutils literal notranslate"><span class="pre">func</span></code> to each matching element.</p>
<p>Note that it is more efficient to create an EPath object and use the select
and apply methods of that object, since this will compile the path string
only once.  This function should only be used as a convenient shortcut for
interactive use.</p>
<p>This is the supported syntax:</p>
<ul class="simple">
<li><dl class="simple">
<dt>select all: <code class="docutils literal notranslate"><span class="pre">/*</span></code></dt><dd><p>Equivalent of <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">arg</span> <span class="pre">in</span> <span class="pre">args:</span></code>.</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>select slice: <code class="docutils literal notranslate"><span class="pre">/[0]</span></code> or <code class="docutils literal notranslate"><span class="pre">/[1:5]</span></code> or <code class="docutils literal notranslate"><span class="pre">/[1:5:2]</span></code></dt><dd><p>Supports standard Python’s slice syntax.</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>select by type: <code class="docutils literal notranslate"><span class="pre">/list</span></code> or <code class="docutils literal notranslate"><span class="pre">/list|tuple</span></code></dt><dd><p>Emulates <code class="docutils literal notranslate"><span class="pre">isinstance()</span></code>.</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>select by attribute: <code class="docutils literal notranslate"><span class="pre">/__iter__?</span></code></dt><dd><p>Emulates <code class="docutils literal notranslate"><span class="pre">hasattr()</span></code>.</p>
</dd>
</dl>
</li>
</ul>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.simplify.epathtools</span> <span class="kn">import</span> <span class="n">epath</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sin</span><span class="p">,</span> <span class="n">cos</span><span class="p">,</span> <span class="n">E</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">t</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span> <span class="o">=</span> <span class="s2">&quot;/*/[0]/Symbol&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">[((</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">2</span><span class="p">),</span> <span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="n">z</span><span class="p">)]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">epath</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">expr</span><span class="p">)</span>
<span class="go">[x, y]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">epath</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">expr</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">expr</span><span class="p">:</span> <span class="n">expr</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">[((x**2, 1), 2), ((3, y**2), z)]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span> <span class="o">=</span> <span class="s2">&quot;/*/*/Symbol&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">t</span> <span class="o">+</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">E</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">epath</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">expr</span><span class="p">)</span>
<span class="go">[x, x, y]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">epath</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">expr</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">expr</span><span class="p">:</span> <span class="mi">2</span><span class="o">*</span><span class="n">expr</span><span class="p">)</span>
<span class="go">t + sin(2*x + 1) + cos(2*x + 2*y + E)</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Simplify</a><ul>
<li><a class="reference internal" href="#id1">simplify</a></li>
<li><a class="reference internal" href="#separatevars">separatevars</a></li>
<li><a class="reference internal" href="#nthroot">nthroot</a></li>
<li><a class="reference internal" href="#kroneckersimp">kroneckersimp</a></li>
<li><a class="reference internal" href="#besselsimp">besselsimp</a></li>
<li><a class="reference internal" href="#hypersimp">hypersimp</a></li>
<li><a class="reference internal" href="#hypersimilar">hypersimilar</a></li>
<li><a class="reference internal" href="#nsimplify">nsimplify</a></li>
<li><a class="reference internal" href="#posify">posify</a></li>
<li><a class="reference internal" href="#logcombine">logcombine</a></li>
<li><a class="reference internal" href="#module-sympy.simplify.radsimp">Radsimp</a><ul>
<li><a class="reference internal" href="#id2">radsimp</a></li>
<li><a class="reference internal" href="#rad-rationalize">rad_rationalize</a></li>
<li><a class="reference internal" href="#collect">collect</a></li>
<li><a class="reference internal" href="#collect-sqrt">collect_sqrt</a></li>
<li><a class="reference internal" href="#collect-const">collect_const</a></li>
<li><a class="reference internal" href="#fraction">fraction</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.simplify.ratsimp">Ratsimp</a><ul>
<li><a class="reference internal" href="#id3">ratsimp</a></li>
<li><a class="reference internal" href="#ratsimpmodprime">ratsimpmodprime</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.simplify.trigsimp">Trigonometric simplification</a><ul>
<li><a class="reference internal" href="#trigsimp">trigsimp</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.simplify.powsimp">Power simplification</a><ul>
<li><a class="reference internal" href="#powsimp">powsimp</a></li>
<li><a class="reference internal" href="#powdenest">powdenest</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.simplify.combsimp">Combinatorial simplification</a><ul>
<li><a class="reference internal" href="#combsimp">combsimp</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.simplify.sqrtdenest">Square Root Denesting</a><ul>
<li><a class="reference internal" href="#sqrtdenest">sqrtdenest</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.simplify.cse_main">Common Subexpression Elimination</a><ul>
<li><a class="reference internal" href="#cse">cse</a></li>
<li><a class="reference internal" href="#opt-cse">opt_cse</a></li>
<li><a class="reference internal" href="#tree-cse">tree_cse</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.simplify.hyperexpand">Hypergeometric Function Expansion</a><ul>
<li><a class="reference internal" href="#hyperexpand">hyperexpand</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.simplify.traversaltools">Traversal Tools</a><ul>
<li><a class="reference internal" href="#use">use</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-sympy.simplify.epathtools">EPath Tools</a><ul>
<li><a class="reference internal" href="#epath-class">EPath class</a></li>
<li><a class="reference internal" href="#epath">epath</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">Simplify</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="hyperexpand.html"
                        title="next chapter">Hypergeometric Expansion</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/simplify/simplify.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="hyperexpand.html" title="Hypergeometric Expansion"
             >next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Simplify"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Simplify</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Simplify</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/simplify/simplify.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:07 GMT -->
</html>